<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/*
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct(){
    	parent::__construct();
		(!session_id()) ? session_start() : '';
		(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in'] && isset($_SESSION['email']))? redirect('profile'):'';
		$this->load->helper(array('url','form'));
		$this->load->helper('myuser_helper');
		$this->load->model('users_model');
    }
	public function index(){
		$cookie_values = array();
		$cookie_values = $_COOKIE;
		$data['cookie'] = $cookie_values;
		$data['title'] = "My Life My Eulogy";
		$this->load->model('Posts_model', '', TRUE);
		$data['ads_sidebar'] = Myuser_helper::getAds('sidebar', 2);
		$qry = $this->Posts_model->getPosts(5);
		$data['latest_posts'] = $qry;
		$data['welcome'] = $this->config->item('welcome');
		$this->load->view('site/home_view', $data);
	}
	function validate_users(){
		$data = array();
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$query = $this->users_model->validate();
		$login_ind = $this->input->post('login_ind');
		$verified = $this->users_model->check_verified_account($email,$password);
		if($query && $verified['is_verified']){ // if the user's credentials validated...
			$this->load->helper('cookie');
			//remember me checkbox
			$remember_me = $this->input->post('remember-me');
			if(!empty($remember_me)){
				//pure PHP cookie
				setcookie('email',$email,time()+86400);//expires in 1 day
				setcookie('password',$password,time()+86400);//expires in 1 day
				setcookie('remember-me',1,time()+86400);//expires in 1 day
				//CI cookie
				$config['sess_cookie_name'] = 'cisession';
				$email = array('name'    => 'email','value'   => $email,'expire'  => time()+86400);
				$password = array('name'    => 'password','value'   => $password,'expire'  => time()+86400);
				$remember_me = array('name'    => 'remember-me','value'   => 1,'expire'  => time()+86400);
				set_cookie($email);
				set_cookie($password);
				set_cookie($remember_me);
			}//end if
			$_SESSION['email'] = $email;
			$_SESSION['is_logged_in'] = TRUE;
			if(!empty($login_ind)){
				echo '1';
			}else{
				redirect('profile');
			}
		}else{ // incorrect username or password
			$post_data = $_POST;
			$error = array();
			if(!empty($login_ind)){
				//2 - free account not verified 3 - vip or reseller account not verified 0 - invalid user login
				if(!$verified['is_verified'] && $query){
					switch(trim(strtolower($verified['type']))){
						case 'free':
							echo '2';
						break;
						default:
							echo '3';
						break;
					}//end switch
				}else{
					echo '0';
				}
			}else{
				// check validation for firstpage
				if(empty($post_data['email']) && empty($post_data['password'])){
					$error[] = empty($post_data['email'])	?	'Email is required'	:	null;
					$error[] = empty($post_data['password'])	?	'Password is required'	:	null;
				}else{
					if(!$query){
						$error[] = 'Invalid Login';
					}else{
						if(!$verified['is_verified']){
							switch(trim(strtolower($verified['type']))){
								case 'free':
									$error[] = 'Please check your email and verify your account first.';
								break;
								default:
									$error[] = 'Please secure your payment first.';
								break;
							}//end switch
						}//end if
					}//end if
				}
				$_SESSION['error'] = $error;
				$_SESSION['post_data'] = $post_data;
				redirect('login');
			}//end if
		}//end if
	}//end fct
}// end of CI_Controller
